<?php

/**
 * [快速打印]
 * @param [any] $array
 */
function P($array){
	dump($array,1,'<pre>',0);
}
/**
 * [判斷是否為空]
 * @param  [any] $value
 * @return [Boolean]
 */
function tep_not_null($value) {
    if (is_array($value)) {
      if (sizeof($value) > 0) {
        return true;
      } else {
        return false;
      }
    } else {
      if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)) {
        return true;
      } else {
        return false;
      }
    }
 }
 /**
  * [過濾數據]
  * @param  [string] $string
  * @return [string]
  */
 function tep_db_prepare_input($string) {
    if (is_string($string)) {
      return trim(tep_sanitize_string(stripslashes($string)));
    } elseif (is_array($string)) {
      reset($string);
      while (list($key, $value) = each($string)) {
        $string[$key] = tep_db_prepare_input($value);
      }
      return $string;
    } else {
      return $string;
    }
  }
  /**
   * [轉譯,過濾]
   * @param  [string] $string
   * @return [string]
   */
function tep_sanitize_string($string) {
    $patterns = array ('/ +/','/[<>]/');
    $replace = array (' ', '_');
    return preg_replace($patterns, $replace, trim($string));
  }
/**
 * [獲取產品的分類]
 * @param  [int] $pid 產品ID
 * @return [string]
 */
function get_products_to_category($pid){
	$cate = D('ProductsRelation')->relation(true)->where(array('products_id'=>$pid))->find();
	//P($cate);
	$cateName ='';
	foreach($cate['categories'] as $v){
		$cateInfo = D('CategoryView')->getOne(array('c.categories_id' => $v['categories_id'],'cd.language_id' => (int)L('lan_id')));
		
		$cateName .= $cateInfo['categories_name'].'<br />';
	}
	return $cateName;
	
}
/**
 * 生成会员随机编码
 */
function randomkeys($length)
{
	$pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
	for($i=0;$i<$length;$i++)
	{
		$key .= $pattern{mt_rand(0,35)};    //生成php随机数
	}
	return $key;
}
/**
 * 生成随机6位验证数
 */
function randyz($length)
{
	$pattern='1234567890';
	for($i=0;$i<$length;$i++)
	{
		$key .= $pattern{mt_rand(0,10)};    //生成php随机数
	}
	return $key;
}
/*
 * 获得查询条件中 社区id
 * 用于社区id 查询条件 和 新增保存记录
 * @param map 查询条件 或 保存数据
 * @type 模型 无type 时，就是查询，有就是数据
 */
function get_map_city_id(&$map, $id='area_id'){
	// 地区筛选
	$cid = I('provcode','');
	$qid = I('citycode','');
	$aid = I('area_id','');
	if($cid > 0){
		$map['provcode'] = $cid;
	}
	if($qid > 0){
		$map['citycode'] = $qid;
	}
	// 社区筛选
	if ($aid > 0) {
		$map[$id] = $aid;
	}
	
	return $map;
}

/**
 * 获取客户端ip地址
 */
function getip() {
	$unknown = 'unknown';
	if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	/*
	 处理多层代理的情况
	 或者使用正则方式：$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
	 */
	if (false !== strpos($ip, ',')) $ip = reset(explode(',', $ip));
	return $ip;
}


?>